#include <iostream>
#include "ManageCamera.h"



ManageCamera::ManageCamera()
{

}


ManageCamera::~ManageCamera()
{
	std::cout << "~~~~ManageCamera ......" << std::endl;

}


bool ManageCamera::CreateCamIpc(std::string ipcName, const char * url)
{
	
	CameraIpc * pCameraIpc = new CameraIpc();
	pCameraIpc->Open(url);

	mapMutex.lock();
	mapCameraIpc.insert(make_pair(ipcName, pCameraIpc));
	mapMutex.unlock();
	return true;
}

bool ManageCamera::CloseCamIpc(std::string ipcName)
{

	mapMutex.lock();
	std::map<std::string, CameraIpc *>::iterator it = mapCameraIpc.find(ipcName);

	if (it != mapCameraIpc.end())
	{
		CameraIpc * pCameraIpc = it->second;

		mapCameraIpc.erase(it);
		pCameraIpc->Close();
		delete pCameraIpc;
	}
	mapMutex.unlock();

	return true;
}

int ManageCamera::GetWidthHeight(std::string ipcName, int & width, int & height)
{
	mapMutex.lock();
	std::map<std::string, CameraIpc *>::iterator it = mapCameraIpc.find(ipcName);
	if (it != mapCameraIpc.end())
	{
		CameraIpc * pCameraIpc = it->second;

		pCameraIpc->GetWidthHeight(width, height);
		
	}
	mapMutex.unlock();
	return 0;
}

unsigned char * ManageCamera::GetImageData(std::string ipcName)
{
	unsigned char * ret = NULL;
	mapMutex.lock();
	std::map<std::string, CameraIpc *>::iterator it = mapCameraIpc.find(ipcName);
	if (it != mapCameraIpc.end())
	{
		CameraIpc * pCameraIpc = it->second;

		ret = pCameraIpc->GetImageData();
	}
	mapMutex.unlock();
	return ret;
}
